<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <style>
         html, body {
            height: 85%;
            margin: 0;
            padding: 0;
            /* background-color: #3372CF; */

        }
        .res {
            position: relative;
            margin-top: 13%;
        }
        .box-card {

            background-color: rgba(8, 114, 167, 0.5);
            color: rgb(202, 255, 244);
            margin: auto auto;
            width: 400px;
            border: 0;
        }
        .login-from {
            margin: auto auto;
        }
        body {
            background-image: url("/static/assets/resbk.png"); /* 确保路径正确 */
            background-repeat: no-repeat;
            background-size: cover;
            background-color: #3372CF;
        }
        .demo-ruleForm .custom-label-style .el-form-item__label {
            color: #ffffff;
        }
        .custom-label-style .el-form-item__label {
            color: rgb(219, 255, 247);
            font-size:1em;
        }
        .dog-img {
            width: 150px;
            max-width: 90%;   /* 小屏幕上自适应 */
            height: auto;
            display: block;   /* 消除图片底部间隙 */
            margin: 0 auto;   /* 居中显示 */
            /* border:red 5px solid; */
            position:absolute;
            top:13%;
            right:35%;
            z-index: 9999;
        }

  /* 添加悬停动画效果 */
        .dog-img:hover {
            transform: scale(1.05);
            transition: transform 0.3s ease;
        }
    </style>
</head>
<body>
    <div id="app">
        <img src="/static/assets/zcdog.png" alt="线条小狗" class="dog-img">
        <div class="res">
            <el-card class="box-card">
                <h2>注册</h2>
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-position="left" label-width="80px" class="demo-ruleForm">
                    <el-form-item label="用户名" class="custom-label-style" prop="uname">
                        <el-input v-model="ruleForm.uname"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" class="custom-label-style" prop="pass">
                        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" class="custom-label-style" prop="password">
                        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div class="btnGroup">
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                    <el-button @click="goBack">返回</el-button>
                </div>
            </el-card>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script>
        new Vue({
            el: '#app',
            data() {
                var validatePass = (rule, value, callback) => {
                    if (value === "") {
                        callback(new Error("请输入密码"));
                    } else {
                        if (this.ruleForm.checkPass !== "") {
                            this.$refs.ruleForm.validateField("checkPass");
                        }
                        callback();
                    }
                };

                var validatePass2 = (rule, value, callback) => {
                    if (value === "") {
                        callback(new Error("请再次输入密码"));
                    } else if (value !== this.ruleForm.pass) {
                        callback(new Error("两次输入密码不一致!"));
                    } else {
                        callback();
                    }
                };
                return {
                    ruleForm: {
                        uname: "",
                        pass: "",
                        password: "",
                    },
                    rules: {
                        uname: [{ required: true, message: "用户名不能为空！", trigger: "blur" }],
                        pass: [{ required: true, validator: validatePass, trigger: "blur" }],
                        password: [{ required: true, validator: validatePass2, trigger: "blur" }],
                    }
                };
            },
            methods: {
                submitForm(formName) {
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            let _this = this;
                            axios({
                                url: "http://127.0.0.1:3000/user/register",
                                method: "post",
                                headers: {
                                    "Content-Type": "application/json",
                                },
                                data: {
                                    uname: _this.ruleForm.uname,
                                    password: _this.ruleForm.password,
                                },
                            }).then((res) => {
                                if (res.data.code === '0') {
                                    this.$message({
                                        message: res.data.msg,
                                        type: "success",
                                    });
                                } else {
                                    this.$message({
                                        message: res.data.msg,
                                        type: "warning",
                                    });
                                }
                                return false;
                            }).catch((error) => {
                                this.$message({
                                    message: '请求失败，请重试',
                                    type: "error",
                                });
                                console.error(error);
                            });
                        } else {
                            console.log("error submit!!");
                            return false;
                        }
                    });
                },
                resetForm(formName) {
                    this.$refs[formName].resetFields();
                },
                goBack() {
                   window.location.href = "http://127.0.0.1:2000";
                },
            },
        });
    </script>
</body>
</html>